package org.taobao.ibatiside.ui.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Properties;

import org.eclipse.datatools.connectivity.IConnectionProfile;
import org.eclipse.datatools.connectivity.ProfileManager;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

public class SelectConnectionProfileDialog extends Dialog {
	private Table table;
	private TableViewer tableViewer;
	private ListViewer listViewer;
	private IConnectionProfile returnProfile;
	static class _Property{
		
		public _Property(String _key, String value) {
			super();
			this._key = _key;
			this.value = value;
		}
		String _key;
		String value;
	}
	public IConnectionProfile getReturnProfile() {
		return returnProfile;
	}

	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public SelectConnectionProfileDialog(Shell parentShell) {
		super(parentShell);
	}
	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Select a connection to use!");
	}
	@Override
	protected void okPressed() {
		returnProfile = (IConnectionProfile) ((IStructuredSelection)listViewer.getSelection()).getFirstElement();
		super.okPressed();
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(2, false));
		
		listViewer = new ListViewer(container, SWT.BORDER | SWT.V_SCROLL);
		List list = listViewer.getList();
		GridData gd_list = new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1);
		gd_list.widthHint = 130;
		list.setLayoutData(gd_list);
		listViewer.setContentProvider(new ArrayContentProvider());
		listViewer.setLabelProvider(new LabelProvider(){
			@Override
			public String getText(Object element) {
				return ((IConnectionProfile)element).getName();
			}
		});
		listViewer.setInput(ProfileManager.getInstance().getProfiles(true));
		listViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				if (tableViewer.getInput() != null){
					((Collection)tableViewer.getInput()).clear();
				}
				tableViewer.setInput(getProperties(event.getSelection()));				
			}
		});
		tableViewer = new TableViewer(container, SWT.BORDER | SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		tableViewerColumn.setLabelProvider(new ColumnLabelProvider() {
			public Image getImage(Object element) {
				return null;
			}
			public String getText(Object element) {
				return element == null ? "" : ((_Property)element)._key;
			}
		});
		TableColumn tblclmnPropertyKey = tableViewerColumn.getColumn();
		tblclmnPropertyKey.setWidth(149);
		tblclmnPropertyKey.setText("Property Key");
		
		TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(tableViewer, SWT.NONE);
		tableViewerColumn_1.setLabelProvider(new ColumnLabelProvider() {
			public Image getImage(Object element) {
				return null;
			}
			public String getText(Object element) {
				return element == null ? "" : ((_Property)element).value;
			}
		});
		TableColumn tblclmnPropertyValue = tableViewerColumn_1.getColumn();
		tblclmnPropertyValue.setWidth(297);
		tblclmnPropertyValue.setText("Property Value");
		tableViewer.setContentProvider(new ArrayContentProvider());
		
		return container;
	}

	protected Object getProperties(ISelection selection) {
		if (!selection.isEmpty()){
			IConnectionProfile profile = (IConnectionProfile) ((StructuredSelection)selection).getFirstElement();
			java.util.List<_Property> props = new ArrayList<SelectConnectionProfileDialog._Property>();
			props.add(new _Property("Name",profile.getName()));
			props.add(new _Property("Description",profile.getDescription()));
			props.add(new _Property("DB Type", profile.getProviderName()));
			Properties baseProperties = profile.getBaseProperties();
			int p;
			for (Object prop_key : baseProperties.keySet()){
				String key = prop_key.toString();
				String removed_fqn_key = (p = key.lastIndexOf('.')) != -1 ? key.substring(p+1) : key;
				props.add(new _Property(removed_fqn_key,baseProperties.get(prop_key).toString()));
			}
			return props;
		}
		return Collections.EMPTY_LIST;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(650, 450);
	}

}
